<template>
<div>
  <el-row v-if="printFlg == false"  class="do-exam-title" style="background-color: #F5F5DC">
    <el-col :span="24">
      <el-row  v-if="answer !== null">
        <el-row  v-if="answer.answerItems !== null">
          <span :key="item.itemOrder"  v-for="item in answer.answerItems">
              <el-tag type="primary" class="do-exam-title-tag" effect="dark" @click="goAnchor('#question-'+item.itemOrder)">{{item.itemOrder}}</el-tag>
          </span>
          </el-row>
        </el-row>
    </el-col>
  </el-row>
  <el-row  class="do-exam-title-hidden">
    <el-col :span="24">
      <el-row  v-if="answer !== null">
        <el-row  v-if="answer.answerItems !== null">
          <span :key="item.itemOrder"  v-for="item in answer.answerItems">
              <el-tag  class="do-exam-title-tag" >{{item.itemOrder}}</el-tag>
          </span>
        </el-row>
      </el-row>
    </el-col>
  </el-row>
  <div  ref="printZone">
    <el-container  class="app-item-contain">
      <el-header class="align-center">
        <h1>{{form.name}}</h1>
        <!-- <div>
          <span class="question-title-padding">试卷得分：{{answer.score}}</span>
          <span class="question-title-padding">试卷耗时：{{formatSeconds(answer.doTime)}}</span>
        </div> -->
      </el-header>
      <el-main>
        <el-form :model="form" ref="form" v-loading="formLoading" label-width="100px">
          <el-row :key="index"  v-for="(titleItem,index) in form.titleItems">
            <h3>{{titleItem.name}}</h3>
            <el-card class="exampaper-item-box" v-if="titleItem.questionItems.length!==0">
              <el-form-item :key="questionItem.itemOrder" :label="questionItem.itemOrder+'.'"
                            v-for="questionItem in titleItem.questionItems"
                            class="exam-question-item"  label-width="50px" :id="'question-'+ questionItem.itemOrder">
              <el-row  v-if="answer !== null">
                <el-row  v-if="answer.answerItems !== null">
                  <el-row  v-if="answer.answerItems.length > 0">
                    <QuestionAnswerShow2 :qType="questionItem.questionType" :question="questionItem"  :answer="answer.answerItems[questionItem.itemOrder-1]"/>
                  </el-row>
                </el-row>
                <el-row  v-if="answer.answerItems === null">
                  <QuestionAnswerShow2 :qType="questionItem.questionType" :question="questionItem"  :answer="{}"/>
                </el-row>
              </el-row>
              <el-row  v-if="answer === null">
                <QuestionAnswerShow2 :qType="questionItem.questionType" :question="questionItem"  :answer="{}"/>
              </el-row>
              </el-form-item>
            </el-card>
          </el-row>
        </el-form>
      </el-main>
    </el-container>
  </div>
  <el-form style="    margin-top: 8px;
  display: flex;
  justify-content: center;
  margin-right: 60px;
  margin-bottom: 50px;" v-show="printFlg == true">
    <el-button plain @click="$router.push('/answer/truelist')">取消</el-button>
    <el-button type="primary" @click="handlePrint">打印</el-button>
  </el-form>
</div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
import { formatSeconds } from '@/utils'
import QuestionAnswerShow2 from './components/QuestionAnswerShow2'
import examPaperAnswerApi from '@/api/examPaperAnwser'
export default {
  components: { QuestionAnswerShow2 },
  data () {
    return {
      printFlg: false,
      form: {},
      formLoading: false,
      fetchCatch: false,
      answer: {
        id: null,
        score: 0,
        doTime: 0,
        answerItems: [
          {
            content: null,
            contentArray: [],
            doRight: null,
            id: null,
            itemOrder: null,
            questionId: null,
            questionScore: null,
            score: null
          }
        ],
        doRight: false
      }
    }
  },
  created () {
    let id = this.$route.query.id
    let printFlg = this.$route.query.pri
    if (printFlg) {
      this.printFlg = true
    }
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      examPaperAnswerApi.read(id).then(re => {
        _this.form = re.response.paper
        _this.answer = re.response.answer
        _this.formLoading = false
      }).catch(err => {
        _this.fetchCatch = true
        console.log('err:', err)
      }).finally(() => {
        if (_this.fetchCatch == true) {
          _this.$message.error('处理异常，请联系管理员或稍后再试！')
        }
        _this.formLoading = false
      })
    }
  },
  methods: {
    handlePrint() {
      this.printFlg = false
      setTimeout(() => {
        this.$nextTick(() => {
          let newDomHtml = this.$refs.printZone.innerHTML
          window.document.body.innerHTML = newDomHtml
          window.print()
        })
        setTimeout(() => {
          this.printFlg = true
        }, 300)

        return
      }, 300)
    },
    formatSeconds (theTime) {
      return formatSeconds(theTime)
    },
    questionDoRightTag (status) {
      return this.enumFormat(this.doRightTag, status)
    },
    goAnchor (selector) {
      this.$el.querySelector(selector).scrollIntoView({ behavior: 'instant', block: 'center', inline: 'nearest' })
    }
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapState('enumItem', {
      doRightTag: state => state.exam.question.answer.doRightTag
    })
  }
}
</script>

<style lang="scss" scoped>
  .align-center {
    text-align: center
  }

  .exam-question-item{
    padding: 10px;
    .el-form-item__label{
      font-size: 15px !important;
    }
  }

  .question-title-padding{
    padding-left: 25px;
    padding-right: 25px;
  }

  .do-exam-title {
    position: fixed;
    width: 100%;
    background: #fff6f6;
    z-index: 999;
    padding: 5px 0px;
  }

  .do-exam-title-hidden {
    width: 100%;
    visibility: hidden;
    padding: 5px 0px;
  }

  .do-exam-title-tag {
    margin-left: 5px;
    cursor: pointer;
  }
</style>
